<?php

class DbSession
{
    protected $dbhelper;
    protected $model;
    public function __construct( $dbhelper, DbModelFactory $model = null )
    {
        $this->dbhelper = $dbhelper;
		if( ! empty( $model ) )
		{
			$this->newModel( $model );
		}
    }
    
    public function newModel( DbModelFactory $model )
    {
        return ( $this->model = $model->buildModel( $this->dbhelper ) );
    }
    
    public function set( $id, $val )
    {
        $this->model->set( $id, $val );
		return $this->model;
    }
    
    public function get( $id )
    {
        return $this->model->get( $id );
    }
    
    public function fetch( $id )
    {
        return $this->model->fetch( $id );				
    }
    
	public function fetchAll( $limit = 0 )
    {
        return $this->model->fetchAll( $limit );
    }
	
	public function fetchAllWithFlag( $id, $limit = 0 )
    {
        return $this->model->fetchAllWithFlag( $id, $limit );
    }
	
	public function fetchWithSelection(  $selection, array $args = array(), $limit = 0 )
    {
        return $this->model->fetchWithSelection( $selection, $args, $limit );
    }
	
    public function insert()
    {
        $this->model->insert();
		
		return $this->model;
    }
    
    public function update()
    {
        $this->model->update();
		return $this->model;
    }
    
    public function remove()
    {
        $this->model->remove();
		
		return $this->model;
    }
}
